﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Interface;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class KetQuaHocRepository:IKetQuaHocRepository
    {
        DangKyPhuEntities entities;

        public KetQuaHocRepository()
        {
            entities = new DangKyPhuEntities();
        }

        /// <summary>
        /// lay cac kqht cua sv theo mon hoc cua tat ca hoc ky
        /// </summary>
        /// <param name="monHocID"></param>
        /// <param name="sinhVienID"></param>
        /// <param name="hockyID"></param>
        /// <returns></returns>
        public List<Entities.KetQuaHoc> LayKetQuaHocTapTheoSVMonHoc(string monHocID, string sinhVienID, IQueryable<int> hockyIDList)
        {
            List<KetQuaHoc> result = new List<KetQuaHoc>();
            foreach (var hocKyItem in hockyIDList)
            {
                var kqht = from m in entities.KetQuaHocs
                           where m.MSSV.Equals(sinhVienID)
                           && m.MonHocID.Equals(monHocID)
                           && m.HocKyID == hocKyItem
                           select m;
                foreach (var kqhtItem in kqht)
                    result.Add(kqhtItem);
            }
            return result;
        }

        /// <summary>
        /// tra ve kqht co dtb mh hoc cao nhat trong list duoc xet
        /// </summary>
        /// <param name="kqhtList"></param>
        /// <returns></returns>
        public KetQuaHoc LayKetQuaCaoNhat(List<KetQuaHoc> kqhtList)
        {
            KetQuaHoc max = kqhtList[0];
            for (int i = 1; i < kqhtList.Count; i++)
            {
                if (kqhtList[i].DTB == null)
                    continue;
                if (kqhtList[i].DTB.Value > max.DTB)
                    max = kqhtList[i];
            }
            return max;
        }




        public IQueryable<KetQuaHoc> LayKetQuaHocTapTheoSVMonHoc(string monHocID, string sinhVienID)
        {
            return (from m in entities.KetQuaHocs
                        where m.MSSV.Equals(sinhVienID)
                        && m.MonHocID.Equals(monHocID)
                        select m);
        }

        /// <summary>
        /// 0: Tất cả
        /// 1: Bắt buộc
        /// 2: Tự chọn
        /// </summary>
        /// <param name="maSV"></param>
        /// <param name="BatBuocTuChon"></param>
        /// <returns></returns>
        public List<KetQuaHoc> DSMonHocTichLuy(string maSV, int BatBuoc_TuChon)
        {
            var dsmhTichLuy = new List<KetQuaHoc>();
            var _kqh = new KetQuaHoc();
            INganhRepository nganhRepo = new NganhDaoTaoRepository();
            INienKhoaRepository nienKhoaRepo = new NienKhoaRepository();
            IHeDaoTaoRepository heDaoTaoRepo = new HeDaoTaoRepository();
            IChuongTrinhDaoTaoRepository ctDaoTaoRepo = new ChuongTrinhDaoTaoRepository();
            NganhDaoTao nganhDaoTao = nganhRepo.LayNganhTuMaSV(maSV);
            NienKhoa nienKhoa = nienKhoaRepo.LayNienKhoaTuMaSV(maSV);
            HeDaoTao heDaoTao = heDaoTaoRepo.LayHeDaoTaoTuMaSV(maSV);
            IQueryable<string> maMonHoc = (ctDaoTaoRepo.LayMonHocChuongTrinhDaoTao(nganhDaoTao.NganhID, nienKhoa.NienKhoaID, heDaoTao.HeDaoTaoID));
            if (BatBuoc_TuChon == 0)
            {
                foreach (var mh in maMonHoc)
                {
                    var kqh = (from m in entities.KetQuaHocs
                               where m.MSSV == maSV
                               && m.MonHocID == mh
                               && m.DTB.Value >= 5
                               select m).Distinct().FirstOrDefault();
                    if (kqh != null)
                        dsmhTichLuy.Add(kqh);
                }
                return dsmhTichLuy;
            }
            else if (BatBuoc_TuChon == 1)
            {

                foreach (var mh in maMonHoc)
                {
                    var kqh = (from k in entities.KetQuaHocs
                               join ctdt in entities.ChuongTrinhDaoTaos
                               on k.MonHocID equals ctdt.MonHocID
                               where k.MSSV == maSV
                               && ctdt.TuChon == false
                               && k.DTB >= 5
                               && ctdt.NganhID == nganhDaoTao.NganhID
                               && ctdt.HeDaoTaoID == heDaoTao.HeDaoTaoID
                               && k.MonHocID == mh
                               select k).Distinct().FirstOrDefault();

                    if (kqh != null && _kqh != kqh)
                    {
                        dsmhTichLuy.Add(kqh);
                    }
                    _kqh = kqh;
                }
                return dsmhTichLuy;
            }
            else if (BatBuoc_TuChon == 2)
            {
                foreach (var mh in maMonHoc)
                {
                    var kqh = (from k in entities.KetQuaHocs
                               join ctdt in entities.ChuongTrinhDaoTaos
                               on k.MonHocID equals ctdt.MonHocID
                               where k.MSSV == maSV
                               && ctdt.TuChon == true
                               && k.DTB >= 5
                               && ctdt.NganhID == nganhDaoTao.NganhID
                               && ctdt.HeDaoTaoID == heDaoTao.HeDaoTaoID
                               && k.MonHocID == mh
                               select k).FirstOrDefault();
                    if (kqh != null && _kqh != kqh)
                    {
                        dsmhTichLuy.Add(kqh);
                    }
                    _kqh = kqh;
                }
                return dsmhTichLuy;
            }
            else
            {
                return null;
            }
        }


        public KetQuaHoc LayMonAVGTCaoNhatDaHT(string maSV)
        {
            MonHocRepository monHocRepo = new MonHocRepository();
            var dsmhAV = monHocRepo.NhomAVGT();
            var dsmhAVdaHT = new List<KetQuaHoc>();
            foreach( var monAV in dsmhAV)
            {
                var avHT = (from m in entities.KetQuaHocs
                            where m.MSSV == maSV
                            && m.MonHocID == monAV.MonHocID
                            && m.DTB >= 5
                            select m).FirstOrDefault();
                if (avHT != null)
                    dsmhAVdaHT.Add(avHT);
            }

            var dsmhDiemGiamDan = (from m in dsmhAVdaHT
                                   orderby m.MonHocID descending
                                   select m);

            return dsmhDiemGiamDan.FirstOrDefault();
        }        
    }
}
